import {
  getOrderDetail,
  sendPayMethod,
  sendShiGongPro,
  sendjoinSeller,
  orderCancel,
  sendNotice,
  workerDel,
  getServicesItems
} from '../../../utils/networkRequest.js'
// pages/jiechedanbox/orderDetail/orderDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderDetail: null,
    isShowReckoning: false, //是否显示结账选择支付方式的弹窗
    isShowShiGongPicker: false, //是否显示选择施工项目的弹窗
    currentPayMethod: null, //当前选择支付方式
    delayNum: "", //当前选择的推送延迟天数 
    orderId: "", //订单id
    userId: "",
    isSelf: false, //判断点击施工人员头像的时候点击的是不是自己。
    servicesItems:[],//选择施工项目时 的项目列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;
    var value = wx.getStorageSync('token') //从本地缓存中获取Session信息
    let userid = ""
    if (value) {
      userid = value.id;
    }
    self.setData({
      orderId: options.id,
      userId: userid
    })


  },
  onShow: function() {
    let self = this;
    getOrderDetail(this.data.orderId, (r) => {
      debugger;
      if (r.code == 0) {
        self.setData({
          orderDetail: r.data,
          currentPayMethod: r.data.payments[0]
        })
      }
    })
  },
  //点击结账标记
  onReckoning: function() {
    this.setData({
      isShowReckoning: true
    })
  },
  //关闭结算窗口
  onReckoningClose: function() {
    this.setData({
      isShowReckoning: false
    })
  },
  //添加施工人员
  onAddShiGong: function(e) {
    let {
      userId,
      orderDetail
    } = this.data;
    let id = e.currentTarget.dataset.id;
    let self = this;
    getServicesItems(orderDetail.id, id, (r) => {
      if (r.code == 0) {
        debugger
        if (userId == id) {
          self.setData({
            isShowShiGongPicker: true,
            isSelf: true,
            servicesItems: r.data
          })
        } else {
          self.setData({
            isShowShiGongPicker: true,
            isSelf: false,
            servicesItems: r.data
          })
        }
      }

    })



  },
  //关闭结算窗口
  onAddShiGongClose: function() {
    this.setData({
      isShowShiGongPicker: false
    })
  },
  //选择支付方式
  pickerPayMethod: function(e) {
    let paymethod = e.currentTarget.dataset.paymethod;

    this.setData({
      currentPayMethod: paymethod
    })
  },
  //获取延迟推送天数
  getDelayChange: function(e) {
    this.setData({
      delayNum: e.detail.detail.value
    })
  },
  //选择施工项目
  onSelectPro: function(e) {
    let {
      servicesItems,
      orderDetail
    } = this.data;
 
    if (this.data.isSelf && !orderDetail.done) {
      let item = e.currentTarget.dataset.item;
      debugger;
      servicesItems.map((o, i) => {
        if (o.id == item.id) {
          o.isSelect = !o.isSelect
        }
      })
      this.setData({
        servicesItems: servicesItems
      })
    }
  },
  //施工项目提交
  onShiGongConfirm: function() {
    let self = this;
    let {
      servicesItems,
      orderDetail
    } = this.data;
    let tempArr = [];
    servicesItems.map((o, i) => {
      if (o.isSelect) {
        tempArr.push(o.service_item_id);
      }
    })
    if (tempArr.length > 0) {
      sendShiGongPro(orderDetail.id, tempArr, (r) => {
        if (r.code == 0) {
          orderDetail.workers = r.data.workers;
          orderDetail.im_in = r.data.im_in;
          self.setData({
            orderDetail: orderDetail,
            isShowShiGongPicker: false
          })
        }
      })
    } else {
      wx.showToast({
        icon: "none",
        title: '请先选择施工项目',
      })
    }

  },
  //销售提交
  addSeller: function() {
    let self = this;
    let {
      orderDetail
    } = this.data;

    wx.showModal({
      title: '提示',
      content: '确定要将自己添加为销售人员吗？',
      success: function(res) {
        if (res.confirm) {
          sendjoinSeller(orderDetail.id, (r) => {
            if (r.code == 0) {
              orderDetail.seller = r.data;
              self.setData({
                orderDetail: orderDetail,
              })
            }
          })
        } else if (res.cancel) {
          //点击取消
        }
      }
    })
  },
  workerDel: function() {
    let self = this;
    let {
      orderDetail
    } = this.data;

    wx.showModal({
      title: '提示',
      content: '确定要将自己从施工列表中剔除吗？',
      success: function(res) {
        if (res.confirm) {
          workerDel(orderDetail.id, (r) => {
            if (r.code == 0) {
              orderDetail.workers = r.data.workers;
              orderDetail.im_in = r.data.im_in;
              self.setData({
                orderDetail: orderDetail,
                isShowShiGongPicker: false
              })
            }
          })
        } else if (res.cancel) {
          //点击取消
        }
      }
    })


  },
  //支付方式提交
  payMethodConfrim: function() {
    let self = this;
    let {
      currentPayMethod,
      delayNum,
      orderDetail
    } = this.data;
    sendPayMethod(orderDetail.id, currentPayMethod.id, delayNum, (r) => {
      if (r.code == 0) {
        self.setData({
          orderDetail: r.data,
          isShowReckoning: false
        })
      }
    })
  },
  //取消订单
  orderCancel: function() {
    let {
      orderDetail
    } = this.data;

    wx.showModal({
      title: '提示',
      content: '确定要取消订单吗？',
      success: function(res) {
        if (res.confirm) {
          orderCancel(orderDetail.id, (r) => {
            if (r.code == 0) {
              wx.navigateBack();
              wx.showToast({
                icon: "none",
                title: '取消成功',
              })
            }else{
              wx.showToast({
                icon:"none",
                title: r.message,
              })
            }
          })
        } else if (res.cancel) {
          //点击取消
        }
      }
    })


  },
  //订单修改
  orderEdit: function() {
    let {
      orderDetail
    } = this.data
    wx.navigateTo({
      url: '../../jiechebox/selectPro/selectPro?isEdit=true&qcode=' + orderDetail.qcode + "&shoppingCartArr=" + JSON.stringify(orderDetail),
    })
  },
  //发送推送
  sendNotice: function() {
    let self = this;
    let {
      orderDetail
    } = this.data
    wx.showModal({
      title: '提示',
      content: '确定要向客户推送订单吗？',
      success: function(res) {
        if (res.confirm) {
          sendNotice(orderDetail.id, (r) => {
            if (r.code == 0) {
              wx.showToast({
                icon: "none",
                title: '发送成功',
              })
            }
          })
        } else if (res.cancel) {
          //点击取消
        }
      }
    })


  }
})